/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eleric.rememome.db.impl;

import com.eleric.rememome.db.AbstractDbRetriever;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.eleric.rememome.db.factory.impl.MemoCategoryDbRetrieverFactory;
import com.eleric.rememome.memo.MemoCategory;
import com.eleric.rememome.memo.impl.MemoCategoryVer1;
import com.eleric.rememome.misc.FieldAndValue;
import com.eleric.rememome.misc.FieldAndValueOperator;
import com.eleric.rememome.type.OrderType;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author MWaldman
 */
public class DbMemoCategoryRetrieverVer1 extends AbstractDbRetriever<MemoCategory>
{
//    public static final String KEY_ROWID = "_id";
//    public static final String KEY_SHORT_NAME = "shortName";
//    public static final String KEY_LONG_NAME = "longName";
//    public static final String[] COLUMNS =
//    {
//        KEY_ROWID,
//        KEY_SHORT_NAME,
//        KEY_LONG_NAME
//    };

    public static final String KEY_ROWID = MemoCategoryDbRetrieverFactory.KEY_ROWID;
    public static final String KEY_SHORT_NAME = MemoCategoryDbRetrieverFactory.KEY_SHORT_NAME;
    public static final String KEY_LONG_NAME = MemoCategoryDbRetrieverFactory.KEY_LONG_NAME;
    public static final String[] COLUMNS = MemoCategoryDbRetrieverFactory.COLUMNS;
    private static final int DATABASE_VERSION = 1;
    private final String DATABASE_CREATE_CAT =
            "create table " + getTable() + " " +
            "(" + KEY_ROWID + " integer primary key autoincrement, " +
            KEY_SHORT_NAME + " text not null, " +
            KEY_LONG_NAME + " text not null" +
            ");";
    private final String DATABASE_INIT_CAT1 =
            "insert into " + getTable() + " " +
            "(" +
            KEY_SHORT_NAME + ", " +
            KEY_LONG_NAME +
            ") " +
            "Values ('TO DO', 'Things to Do.');";
    private final String DATABASE_INIT_CAT2 =
            "insert into " + getTable() + " " +
            "(" +
            KEY_SHORT_NAME + ", " +
            KEY_LONG_NAME +
            ") " +
            "Values ('TO GET', 'Things to Get.');";

    public DbMemoCategoryRetrieverVer1(String table)
    {
        super(table);
    }

    @Override
    protected MemoCategory convertTo(Cursor c, SQLiteDatabase db)
    {
        MemoCategory mc;
        if (c != null)
        {
            mc = new MemoCategoryVer1();
            mc.setId(c.getInt(0));
            mc.setShortName(c.getString(1));
            mc.setLongName(c.getString(2));
            return mc;
        }
        else
        {
            return null;
        }
    }

    @Override
    protected String[] getColumns()
    {
        return COLUMNS;
    }

    @Override
    protected String getOrderTypeString(OrderType orderType)
    {
        String order;
        switch (orderType)
        {
            case ALPHA:
                order = KEY_SHORT_NAME + " COLLATE NOCASE";
                break;
            case NEWEST:
                order = KEY_ROWID + " DESC";
                break;
            case OLDEST:
                order = KEY_ROWID;
                break;
            case PRIORITY:
                order = null;
                break;
            default:
                order = null;
                break;
        }
        return order;
    }

    public boolean delete(MemoCategory cat, SQLiteDatabase db)
    {
        List<FieldAndValueOperator> fieldValueOperList;
        fieldValueOperList = new ArrayList<FieldAndValueOperator>();
        fieldValueOperList.add(new FieldAndValueOperator(KEY_ROWID, cat.getId(), "="));
        return delete(fieldValueOperList, db);
    }

    public long insert(MemoCategory cat, SQLiteDatabase db)
    {
        List<FieldAndValue> fieldAndValues;

        fieldAndValues = new ArrayList<FieldAndValue>();
        if (cat.getId() > 0)
        {
            fieldAndValues.add(new FieldAndValue(KEY_ROWID, cat.getId()));
        }
        fieldAndValues.add(new FieldAndValue(KEY_SHORT_NAME, cat.getShortName()));
        fieldAndValues.add(new FieldAndValue(KEY_LONG_NAME, cat.getLongName()));
        return insert(fieldAndValues, db);
    }

    public boolean update(MemoCategory cat, SQLiteDatabase db)
    {
        List<FieldAndValue> fieldAndValues;
        List<FieldAndValueOperator> fieldValueConditions;

        fieldAndValues = new ArrayList<FieldAndValue>();
        fieldValueConditions = new ArrayList<FieldAndValueOperator>();
        fieldAndValues.add(new FieldAndValue(KEY_SHORT_NAME, cat.getShortName()));
        fieldAndValues.add(new FieldAndValue(KEY_LONG_NAME, cat.getLongName()));
        fieldValueConditions.add(new FieldAndValueOperator(KEY_ROWID, cat.getId(), "="));
        return update(fieldAndValues, fieldValueConditions, db);
    }

    public int getVersion()
    {
        return DATABASE_VERSION;
    }

    public String createTableSQL()
    {
        return DATABASE_CREATE_CAT;
    }

    public String[] initializeTableSQL()
    {
        String[] init;
        init = new String[2];
        init[0] = DATABASE_INIT_CAT1;
        init[1] = DATABASE_INIT_CAT2;
        return init;
    }

    @Override
    public String getKeyField()
    {
        return KEY_ROWID;
    }

    public String[] buildTableRestraintsSQL()
    {
        return null;
    }

    public String[] removeTableRestraintsSQL()
    {
        return null;
    }
}
